﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using UtilLib;
using UtilPublic.ServerLoger;
using ServerBase.DataBaseRedis;
using ServerBase.Protocol;
using ServerBase.Other;
using System.Collections.Generic;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using static ServerGame.Service.Dispatcher;
using UtilPublic.Serialization;
using ServerGame.Service;
using static ServerBase.Config.Conf;
using ServerBase.Config;
using System;
using ServerBase.IniFile;
using System.Collections.Concurrent;
using ServerGame.GameObject;
using ServerPublic.Manager;
using ServerPublic.DataBaseMysql;

namespace ServerGame.Manager
{
    /// <summary>
    /// 玩家管理器
    /// </summary>
    public static class PlayerMgr
    {
        // 判断属性
        public static bool ValueCheck(this Player player, EAwardType attribute, long value)
        {
            switch (attribute)
            {
                case EAwardType.金币:
                    return (player.DaBasic.Gold >= value);
                case EAwardType.钻石:
                    return (player.DaBasic.Gem >= value);
                case EAwardType.经验:
                    return (player.DaCommon.Exp >= value);
                case EAwardType.潜能:
                    return (player.DaBasic.Pot >= value);
                default:
                    return false;
            }
        }
        //增加玩家属性
        public static bool ValueAdd(this Player player, EAwardType attribute, long value, EActionSource source)
        {
            loger.Debug($"PlayerManager.ValueAdd player{player.Uid} attribute{attribute} value{value} source{source}");

            if (value < 0 && source != EActionSource.GM)
            {
                loger.Error($"PlayerManager.AddValue error {attribute} value{value} source={source}");
                return false;
            }
            if (value == 0)
            {
                return true;
            }
            switch (attribute)
            {
                case EAwardType.金币:
                    player.DaBasic.Gold += value;
                    (ERecordType.产出记录金币, source).RecValue(player.Puid, value);
                    break;
                case EAwardType.钻石:
                    player.DaBasic.Gem += value;
                    (ERecordType.产出记录钻石, source).RecValue(player.Puid, value);
                    break;
                case EAwardType.经验:
                    player.DaCommon.Exp += value;
                    player.LevelSetup();
                    (ERecordType.产出记录经验, source).RecValue(player.Puid, value);
                    break;
                case EAwardType.潜能:
                    player.DaBasic.Pot += value;
                    (ERecordType.产出记录潜能, source).RecValue(player.Puid, value);
                    break;
                default:
                    loger.Error($"PlayerManager.AddValue Puid{player.Uid} error {attribute} value{value} source={source}");
                    return false;
            }
            return true;
        }
        //减少玩家属性
        public static bool ValueUse(this Player player, EAwardType attribute, long value, EActionSource source)
        {
            loger.Debug($"PlayerManager.UseValue player{player.Uid} attribute{attribute} value{value} source{source}");
            if (value < 0)
            {
                loger.Error($"PlayerManager.UseValue error Puid{player.Uid} {attribute} value{value} source={source}");
                return false;
            }
            if (value == 0)
            {
                return true;
            }
            switch (attribute)
            {
                case EAwardType.金币:
                    player.DaBasic.Gold -= value;
                    (ERecordType.消耗记录金币, source).RecValue(player.Puid, value);
                    break;
                case EAwardType.钻石:
                    player.DaBasic.Gem -= value;
                    (ERecordType.消耗记录钻石, source).RecValue(player.Puid, value);
                    break;
                case EAwardType.经验:
                    player.DaCommon.Exp -= value;
                    player.LevelSetup();
                    (ERecordType.消耗记录经验, source).RecValue(player.Puid, value);
                    break;
                case EAwardType.潜能:
                    player.DaBasic.Pot -= value;
                    (ERecordType.消耗记录潜能, source).RecValue(player.Puid, value);
                    break;
                default:
                    loger.Error($"PlayerManager.UseValue Puid{player.Uid} {attribute} value{value} source={source}");
                    return false;
            }
            return true;
        }


    }
}